package com.itheima.vmmysql.Controller;

import com.itheima.vmmysql.Service.TableDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TableDataController {

    @Autowired
    private TableDataService tableDataService;

    @GetMapping("/table")
    public String getTableData(@RequestParam("name") String tableName, Model model) {
        // 获取指定表的数据
        List<String> tableData = tableDataService.getTableData(tableName);

        // 将数据和表名添加到模型中
        model.addAttribute("tableData", tableData);
        model.addAttribute("tableName", tableName);

        // 检查数据是否为空
        if (tableData == null || tableData.isEmpty()) {
            return "html/error";  // 数据为空时返回错误页面
        }

        return "html/table";  // 数据存在时返回表格展示页面
    }
}
